using Godot;
using System;
using System.Collections.Generic;

public class Console : Control
{
    public RichTextLabel label;
    public LineEdit Edit;
    int index = 0;
    public float LastTime = 0;
    List<String> LastMessages = new List<string>();
    List<CommandaData> Commands = new List<CommandaData>();
    public override void _Ready()
    {
        label = GetNode<RichTextLabel>("RichTextLabel");
        Edit = GetNode<LineEdit>("RichTextLabel/Edit");
        AddCommand(new CommandaData("help", (List<string> datas) =>
        {
            SendMessage($"1.gamemode [0/1] 更换游戏模式", false);
            return true;
        }));
    }
    public override void _Process(float delta)
    {
        if (Visible)
            if (Input.IsActionJustPressed("esc"))
            {
                Visible = false;
            }
    }
    public void KeyInput(InputEvent e)
    {
        if (e.IsActionPressed("up"))
        {
            if (index < LastMessages.Count - 1) index++;
            Edit.Text = LastMessages[index];
            Edit.GrabFocus();
            Edit.CaretPosition = Edit.Text.Length;
        }
        if (e.IsActionPressed("down"))
        {
            if (index > 0) index--;
            Edit.Text = LastMessages[index];
            Edit.GrabFocus();
            Edit.CaretPosition = Edit.Text.Length;
        }
    }
    public void SendMessage(String Text, bool prefix = true)
    {
        if (prefix) label.BbcodeText += $"{DateTime.Now.ToString("HH:mm")} > {Text}\n";
        else label.BbcodeText += $"{Text}\n";
        LastMessages.Add(Text);
    }
    public void OnSendMessageEvent(String Text)
    {
        if (Text == "") return;
        index = LastMessages.Count;
        Edit.Text = "";
        SendMessage(Text);
        if (Text.Contains(" ") && Text[0] != ' ')
        {
            String[] str = Text.Split(" ");
            String[] data = new String[str.Length];
            if (str.Length >= 2)
            {
                for (int i = 0; i < str.Length - 1; i++)
                {
                    data[i] = str[i + 1];
                }
                Execute(str[0], data);
            }
            else
            {
                String[] args = new String[1];
                args[0] = str[1];
                Execute(str[0], args);
            }

        }
        else
        {
            Execute(Text, null);
        }
    }
    public void Execute(String label, String[] data)
    {
        for (int i = 0; i < Commands.Count; i++)
        {
            if (Commands[i].Label.ToUpper() == label.ToUpper())
            {
                if (Commands[i].OnExecute != null)
                {
                    List<String> p = null;
                    if (data != null) p = new List<string>(data);
                    if (Commands[i].OnExecute(p))
                    {

                    }
                    else
                    {
                        SendMessage(Commands[i].wrongs.Color("red"), false);
                    }
                }
            }

        }
    }
    public void AddCommand(CommandaData oc)
    {
        Commands.Add(oc);
    }
    public void open()
    {
        Visible = true;
        Edit.GrabFocus();
    }
    public void close()
    {
        Visible = false;
    }
}
